/**   
 * @Title: BuyingCommunicater.java 
 * @Package com.esun.rabbit2.communicaters.shiyixuanwu 
 * @Description: 十一选五购买交互
 * @Author: Miko baoz@500wan.com   
 * @Date: 2011-6-29 下午05:24:15  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.communicaters.shiyixuanwu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;

import com.esun.config.BussinessCode;
import com.esun.config.Config;
import com.esun.config.DistributionChannels;
import com.esun.config.Key;
import com.esun.model.UserInfo;
import com.esun.network.ServerCommunicater;

/**
 * @ClassName: BuyingCommunicater
 * @Description: 十一选五购买交互类
 * @Author: Miko baoz@500wan.com
 * @Date: 2011-6-29 下午05:24:15
 * 
 */
public class BuyingCommunicater extends ServerCommunicater {

	private ArrayList<Hashtable<String, String>> alCache;

	public boolean buy(ArrayList<String> buyData, boolean isBingoStop, String money, String currentTerm, int multiple, int follow) {

		ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();

		list.add(mapItem(BussinessCode.PASER_MID, BussinessCode.MID_SHIYIXUANWU_BUY));
		list.add(mapItem(BussinessCode.PASER_CK, UserInfo.getUserInfo().getUserSession()));

		if (isBingoStop) {
			list.add(mapItem(Key.KEY_ZJCUT, Config.NUM1));
		} else {
			list.add(mapItem(Key.KEY_ZJCUT, Config.NUM0));
		}

		list.add(mapItem(Key.KEY_ALLMONEY, money));

		StringBuilder sbTermList = new StringBuilder();
		sbTermList.append(currentTerm);

		StringBuilder sbMultipel = new StringBuilder();

		for (int i = 0; i < follow; i++) {
			sbMultipel.append(multiple).append(Config.COMMA);
		}
		sbMultipel.setLength(sbMultipel.length() - 1);

		list.add(mapItem(Key.KEY_EXPECT, sbTermList.toString()));
		list.add(mapItem(Key.KEY_NUM, String.valueOf(follow)));
		list.add(mapItem(Key.KEY_BEISHULIST, sbMultipel.toString()));
		list.add(mapItem(Key.KEY_CHANNEL, DistributionChannels.QUDAO));

		list.add(mapItem(Key.KEY_LIST, rowOutput2(buyData)));

		try {
			super.getServerResponse(list, true);

			if (super.getState() == 100) {

				updateLocalCach();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}
	}

	private void updateLocalCach() {
		alCache = super.getListResult();
	}

	public Hashtable<String, String> getBuyInfo() {
		return alCache.get(0);
	}

	/**
	 * City临时提供的组织row方法 该方法应放入通讯类 XML 创建类中
	 **/
	private String rowOutput2(ArrayList<String> list) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < list.size(); i++) {
			String value = ((String) list.get(i));
			sb.append("<").append("row").append(" ").append("code").append("=").append("\"").append(value).append("\"").append("/").append(">");
		}
		String result = sb.toString();
		return result;

	}

	private HashMap<String, String> mapItem(String name, String value) {
		HashMap<String, String> map = new HashMap<String, String>();
		map.put(name, value);
		return map;
	}

	public String getBalance() {

		String balance = super.getResult().get(Key.KEY_USERMONEY);

		UserInfo.getUserInfo().setUserBalance(balance);

		return balance;
	}

}
